package ch.ethz.inf.shop;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import ch.ethz.inf.shop.model.DatastoreInterface;
import ch.ethz.inf.shop.model.DatastoreInterfaceSimpleDatabase;
import ch.ethz.inf.shop.model.RegisteredUser;
import ch.ethz.inf.shop.model.ShoppingCartLine;
import ch.ethz.inf.shop.util.UserManagement;
import ch.ethz.inf.shop.util.html.BeanTableHelper;

/**
 * Servlet implementation class HomePage
 */
@WebServlet(description = "The home page of the project", urlPatterns = { "/ShoppingCart" })
public final class ShoppingCartServlet extends HttpServlet {
	private static final Logger LOGGER = Logger.getLogger(ShoppingCartServlet.class);

	private static final long serialVersionUID = 1L;
	private final DatastoreInterface dbInterface = new DatastoreInterfaceSimpleDatabase();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {

		try {
			final HttpSession session = request.getSession(true);
			RegisteredUser user = UserManagement.getCurrentlyLoggedInUser(session);
			if (user == null) {
				this.getServletContext().getRequestDispatcher("/User.jsp").forward(request, response);
				return;
			}

			final String add = request.getParameter("add");
			if (add != null) {
				dbInterface.addOneToShoppingCartLine(Integer.parseInt(request.getParameter("cartLineId")));
			}

			final String remove = request.getParameter("remove");
			if (remove != null) {
				dbInterface.removeOneFromShoppingCartLine(Integer.parseInt(request.getParameter("cartLineId")));
			}

			final BeanTableHelper<ShoppingCartLine> shoppingCartLinesTable = new BeanTableHelper<ShoppingCartLine>("cartLineTable", "productsTable",
					ShoppingCartLine.class);

			shoppingCartLinesTable.addBeanColumn("Product Name", "name");
			shoppingCartLinesTable.addBeanColumn("Product Weight", "weight");
			shoppingCartLinesTable.addBeanColumn("Product Country of Origin", "countryOfOrigin");
			shoppingCartLinesTable.addBeanColumn("Product Category", "category");
			shoppingCartLinesTable.addBeanColumn("Product Brand", "brand");

			shoppingCartLinesTable.addLinkColumn("", "<img src=\"images/document-open.png\" alt=\"View Product\"/>", "Product?productId=", "productId");
			shoppingCartLinesTable.addLinkColumn("", "<img src=\"images/list-add.png\" alt=\"Add one\"/>", "ShoppingCart?add=true&cartLineId=", "cartLineId");
			shoppingCartLinesTable.addLinkColumn("", "<img src=\"images/list-remove.png\"alt=\"Remove one\"/>", "ShoppingCart?remove=true&cartLineId=",
					"cartLineId");
			shoppingCartLinesTable.addBeanColumn("Amount", "amount");
			shoppingCartLinesTable.addBeanColumn("Price", "linePrice");

			// Todo remove one entry

			shoppingCartLinesTable.addObjects(dbInterface.getShoppingCartLinesForUser(user));

			shoppingCartLinesTable.setVertical(false);

			session.setAttribute("cartLineTable", shoppingCartLinesTable);

			session.setAttribute("totalPrice", dbInterface.getShoppingCartPriceForUser(user));

			this.getServletContext().getRequestDispatcher("/ShoppingCart.jsp").forward(request, response);
		} catch (Exception e) {
			LOGGER.error("", e);
			this.getServletContext().getRequestDispatcher("/User.jsp").forward(request, response);
		}

	}

}
